package com.danco.training.zaneuskaya.faces;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.danco.training.zaneuskaya.domain.City;
import com.danco.training.zaneuskaya.domain.Country;
import com.danco.training.zaneuskaya.domain.Event;
import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.service.EventService;
import com.danco.training.zaneuskaya.service.UserService;
import com.danco.training.zaneuskaya.service.exception.UserException;

/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Component
@Scope("session")
public class EventBean implements Serializable {

	private static final String NO_CURRENT_EVENT_OR_USER_NOT_LOGGED_IN = "No current event or user not logged in ";

	private static final String EVENT_INFO_FACES_REDIRECT_TRUE_PAGE = "/event/info?faces-redirect=true";

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static final Logger LOGGER = Logger.getLogger(EventBean.class);

	private String title;

	private String description;

	private int eventId;

	private Date startTime;

	private Date endTime;

	private String address;

	private Country country;

	private City city;

	private List<Event> eventList;

	private List<User> eventMembers;

	private boolean member;

	private Event currentEvent;

	private User eventOrganizer;

	@Autowired
	private UserBean userBean;

	@Autowired
	private UserService userService;

	@Autowired
	private EventService eventService;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Country getCountry() {
		return country;
	}

	public void setCountry(Country country) {
		this.country = country;
	}

	public City getCity() {
		return city;
	}

	public void setCity(City city) {
		this.city = city;
	}

	public boolean isMember() {
		if (currentEvent == null || userBean.getSelf() == null) {
			LOGGER.info(NO_CURRENT_EVENT_OR_USER_NOT_LOGGED_IN);
			return false;
		} else {
			return eventService.isParticipant(currentEvent, userBean.getSelf());
		}

	}

	public void setMember(boolean member) {
		this.member = member;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public User getEventOrganizer() {
		eventOrganizer = userService.getEntityById(currentEvent.getOrganizer()
				.getUserId());
		return eventOrganizer;
	}

	public void setEventOrganizer(User eventOrganizer) {
		this.eventOrganizer = eventOrganizer;
	}

	public List<User> getEventMembers() {
		eventMembers = userService.getEventMembers(currentEvent);
		return eventMembers;
	}

	public void setEventMembers(List<User> eventMembers) {
		this.eventMembers = eventMembers;
	}

	public Event getCurrentEvent() {
		return currentEvent;
	}

	public int getEventId() {
		return eventId;
	}

	public void setEventId(int eventId) {
		currentEvent = eventService.getEntityById(eventId);
		this.eventId = eventId;
	}

	public void setCurrentEvent(Event currentEvent) {
		this.currentEvent = currentEvent;
	}

	public List<Event> getEventList() {
		eventList = eventService.getEntityList();
		return eventList;
	}

	public void setEventList(List<Event> eventList) {
		this.eventList = eventList;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public EventService getEventService() {
		return eventService;
	}

	public void setEventService(EventService eventService) {
		this.eventService = eventService;
	}

	public String addMember() {
		eventService.addEventParticipant(currentEvent, userBean.getSelf());
		return EVENT_INFO_FACES_REDIRECT_TRUE_PAGE;
	}

	public String addEvent() {
		if (!userBean.getSelf().equals(null)) {
			Event event = new Event();
			event.setEventAddress(address);
			event.setEventCity(city);
			event.setEventCountry(country);
			event.setOrganizer(userBean.getSelf());
			event.setEventStartDate(startTime);
			event.setEventEndDate(endTime);
			event.setEventTitle(title);
			event.setEventDescription(description);
			try {
				eventService.createEntity(event);
			} catch (UserException e) {
				LOGGER.error(e.getMessage(), e);
			}
			setCurrentEvent(event);
		}
		return EVENT_INFO_FACES_REDIRECT_TRUE_PAGE;
	}

}
